const Router = require('koa-router')
const Flow = require('@models/flow')
const router = new Router({
    prefix: '/v1/classic'
})
const { Art } = require('@models/art')
// 别名 alias module-alias
const {
    Auth
} = require('@root/middlewares/auth')

const {
    PositiveIntegerValidator,
    LikeValidator
} = require('../../validators/validator.js')

const { Favor } = require('@models/favor.js')

// 获取最新
router.get('/latest', new Auth().m, async (ctx, next) => {
    // ctx.body  = ctx.auth.uid
    // 权限
    // 分级 scope 
    // 用数字来表示令牌
    const flow = await Flow.findOne({
        order: [
            ['index','DESC']
        ]
    })
    const art = await Art.getData(flow.art_id, flow.type, false)
    const likeLatest = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
    // art.dataValues.index = flow.index
    art.setDataValue('index', flow.index)
    art.setDataValue('like_status', likeLatest)
    ctx.body = art
})

// // 下一期
router.get('/:index/next', new Auth().m, async (ctx) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'index'
    })
    const index = v.get('path.index')
    const flow = await Flow.findOne({
        where: {
            index: index + 1
        }
    })
    if(!flow) {
        throw new global.errs.NotFound()
    }
    const art = await Art.getData(flow.art_id, flow.type, false)
    const likeLatest = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
    // art.dataValues.index = flow.index
    art.setDataValue('index', flow.index)
    art.setDataValue('like_status', likeLatest)
    ctx.body = art
})

// 上一期
router.get('/:index/previous', new Auth().m, async (ctx) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'index'
    })
    const index = v.get('path.index')
    const flow = await Flow.findOne({
        where: {
            index: index - 1
        }
    })
    if(!flow) {
        throw new global.errs.NotFound()
    }
    const art = await Art.getData(flow.art_id, flow.type, false)
    const likeLatest = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
    // art.dataValues.index = flow.index
    art.setDataValue('index', flow.index)
    art.setDataValue('like_status', likeLatest)
    ctx.body = art
})

// 获取喜欢情况
router.get('/:type/:id/favor', new Auth().m, async ctx => {
    const v = await new LikeValidator().validate(ctx)
    const id = v.get('path.id')
    const type =parseInt(v.get('path.type'))
    const art = await Art.getData(id, type)
    if(!art) {
        throw new global.errs.NotFound()
    }
    const like = await Favor.userLikeIt(id, type, ctx.auth.uid)
    ctx.body = {
        fav_nums: art.fav_nums,
        like_status: like
    }
})

// 获取用户的点赞信息
router.get('/favor', new Auth().m, async ctx => {
    const uid = ctx.auth.uid
    ctx.body = await Favor.getMyClassicFavors(uid)
    
})

// 获取期刊的信息
router.get('/:type/:id', new Auth().m, async ctx => {
    const v = await new LikeValidator().validate(ctx)
    const id = v.get('path.id')
    const type =parseInt(v.get('path.type'))
    const art = await Art.getData(id, type)
    if(!art) {
        throw new global.errs.NotFound()
    }
    const like = await Favor.userLikeIt(id, type, ctx.auth.uid)
    art.setDataValue('like_status', like)
    ctx.body = art
})


module.exports = router
